package com.lorenzogao.latte_core.ui

import android.content.Context
import android.view.Gravity
import androidx.appcompat.app.AppCompatDialog
import com.lorenzogao.latte_core.R
import com.lorenzogao.latte_core.ext.getScreenHeight
import com.lorenzogao.latte_core.ext.getScreenWidth

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2020-01-08 16:00
 *  description:
 */


class LatteLoader {


    companion object {


        val LOADER_SIZE_SCALE=8

        val LOADER_OFFSET_SCALE=10

        val LOADERS= mutableListOf<AppCompatDialog>()


        fun showLoading(context: Context, type: String=LoaderStyle.BallClipRotatePulseIndicator.name) {

            val dialog = AppCompatDialog(context, R.style.dialog)

            val avLoadingIndicatorView = LoaderCreator.create(type, context)
            dialog.setContentView(avLoadingIndicatorView)

            val deviceWidth = context.getScreenWidth()
            val deviceHeight = context.getScreenHeight()

            dialog.window?.apply {
                val lp = attributes
                lp.width=deviceWidth/LOADER_SIZE_SCALE
                lp.height=deviceHeight/LOADER_SIZE_SCALE
                lp.height=lp.height+deviceHeight/LOADER_OFFSET_SCALE
                lp.gravity=Gravity.CENTER
            }
            LOADERS.add(dialog)
            dialog.show()
        }


        fun stopLoading(){
            for (dialog in LOADERS) {
                dialog?.let {
                    if (dialog.isShowing){
                        dialog.cancel()
                        dialog.dismiss()
                    }
                }
            }
        }

    }






}